package com.yth.extension;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor;

import java.lang.reflect.Constructor;

/**
 * bean的推断
 *
 * <li>注意：<code>SmartInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessor</code></li>
 *
 * @author yutianhong
 * @version 1.0
 * @since 2023/08/29 16:03:24
 */
@Slf4j
public class TestSmartInstantiationAwareBeanPostProcessor implements SmartInstantiationAwareBeanPostProcessor {
    @Override
    public Class<?> predictBeanType(Class<?> beanClass, String beanName) throws BeansException {
        // 定制bean的类型
        log.info("[SmartInstantiationAwareBeanPostProcessor::predictBeanType] \t>>> beanClass: {}, beanName: {}", beanClass.getSimpleName(), beanName);
        return null;
    }

    @Override
    public Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, String beanName) throws BeansException {
        // 在postProcessBeforeInstantiation之后，实例化之前，可以自定义bean创建用的构造器
        log.info("[SmartInstantiationAwareBeanPostProcessor::determineCandidateConstructors] \t>>> beanClass: {}, beanName: {}", beanClass.getSimpleName(), beanName);
        return null;
    }

    @Override
    public Object getEarlyBeanReference(Object bean, String beanName) throws BeansException {
        // 在初始化期间，获得早期bean，用于'循环依赖'场景
        log.info("[SmartInstantiationAwareBeanPostProcessor::getEarlyBeanReference] \t>>> beanClass: {}, beanName: {}", bean.getClass().getSimpleName(), beanName);
        return bean;
    }
}
